# Contributor: Steeve Chailloux <steeve.chailloux@orus.io>
# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Contributor: Mitch Tishmack <mitch.tishmack@gmail.com>
# Maintainer: Sören Tempel <soeren+alpine@soeren-tempel.net>
pkgname=cabal
pkgver=3.10.2.1
pkgrel=0
pkgdesc="The Haskell Cabal"
url="https://haskell.org/cabal"
arch="aarch64 x86_64" # Limited by GHC
license="BSD-3-Clause"
depends="ghc gmp zlib curl"
makedepends="ghc>=9.4.6 gmp-dev libffi-dev zlib-dev cabal-bootstrap"
options="net !check" # TODO: enable tests
subpackages="$pkgname-doc"
source="https://hackage.haskell.org/package/cabal-install-$pkgver/cabal-install-$pkgver.tar.gz
	cabal.project.freeze"
builddir="$srcdir/cabal-install-$pkgver"

# We currently don't package Haskell dependencies in aports. As such,
# building cabal requires a pre-existing cabal version to download all
# dependencies of the cabal package itself. Presently, this is achieved
# through the separate cabal-stage0 package which bootstraps cabal
# from source using a provided Python script. From that point onward,
# we can use the previous version of cabal to compile cabal.
#
# For this reason, both community/cabal-stage0 and community/cabal
# provide the virtual cabal-bootstrap package but the former has the
# lower priority.
#
# See also https://lists.alpinelinux.org/~alpine/devel/%3C33KG0XO61I4IL.2Z7RTAZ5J3SY6%408pit.net%3E
provides="cabal-bootstrap"
provider_priority=100 # highest

# Directory were cabal files are stored.
_cabal_home="$srcdir/dist"

cabal_update() {
	cd $builddir
	# Build a freeze file to make the build reproducible.
	# This freeze file is stored in $source and thus tracked in Git.
	HOME="$_cabal_home" cabal v2-update
	(
		cd "$builddir"
		HOME="$_cabal_home" cabal v2-freeze \
			--shadow-installed-packages
		mv cabal.project.freeze "$startdir/"
	)
}

prepare() {
	default_prepare
	ln -sf "$srcdir/cabal.project.freeze" \
		"$builddir/cabal.project.freeze"
}

build() {
	# ghc version path
	export PATH="$PATH:/usr/lib/llvm14/bin"
	HOME="$_cabal_home" cabal v2-update
	HOME="$_cabal_home" cabal v2-build cabal-install:exes \
		--jobs=${JOBS:-1} \
		--prefix=/usr \
		--docdir=/usr/share/doc/$pkgname \
		--sysconfdir=/etc
}

package() {
	# With v2- cabal no longer wants us to separate v2-build and
	# v2-install, however, we don't want to build everything in a
	# fakeroot. We work around this by copying binaries build in the
	# previous step manually.
	#
	# See https://github.com/haskell/cabal/issues/6919#issuecomment-761563498
	HOME="$_cabal_home" cabal list-bin cabal-install:exes | \
		xargs install -Dm755 -t "$pkgdir"/usr/bin

	mkdir -p "$pkgdir"/usr/share/man/man1
	HOME="$_cabal_home" cabal man --raw \
		> "$pkgdir"/usr/share/man/man1/cabal.1

	install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

sha512sums="
f87f3872e385d50b056dbdc6acda32b333743e24e36e3cffed02d088d83a37d9b44dcba43908e7f4cd0a83ec4802350b915c021991e3e6806bf7f4a0c99b6890  cabal-install-3.10.2.1.tar.gz
7098aac68a8208ee9e9cf606aad93b0110d3d5ed67189a7896b4a618bf016d2bf42181b87170b752433074357c230741f753ffbc2e0ba8eb21a410014913877c  cabal.project.freeze
"
